//
//  ByViewLoginUserOperation.m
//  jinpuyiyuan_ios
//
//  Created by pro on 2017/12/8.
//  Copyright © 2017年 mc. All rights reserved.
//

#import "ByViewLoginUserOperation.h"

// vc
#import "ByForgetVC.h"

// tabbar
#import "ByTabBarUser.h"
#import "ByTabBarDoctor.h"

// api
#import "ByApiUser.h"

@implementation ByViewLoginUserOperation
- (instancetype)init{
    if (self = [super init]) {
        self.layer.cornerRadius = 4.0;
        self.backgroundColor = [UIColor whiteColor];
        [self createViews];
    }
    return self;
}

- (void)createViews{
    NSArray *places = @[@"手机号码",@"密码"];
    CGFloat margin = (SCREEN_HEIGHT / 2 - 100 - 30 * 2 - 20) / 2 - 30;
    CGFloat forget_width = [@"忘记密码" getSizeOfString:15].width + 10;
    for (int i = 0; i < places.count; i ++) {
        UITextField *tf = [[UITextField alloc] init];
        [self addSubview:tf];
        [tf mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(30));
            make.top.equalTo(@(margin + 30 * i + 20 * i));
            if (i == 0) {
                make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 30 - 60, 30));
            }else{
                make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 30 - 60 - forget_width, 30));
            }
        }];
        tf.placeholder = places[i];
        tf.font = FONT(15);
        tf.tag = 10 + i;
        
        //  line
        UIView *line = [[UIView alloc] init];
        [self addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(30));
            make.top.equalTo(tf.mas_bottom);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 30 - 60, 1));
        }];
        line.backgroundColor = BY_THEME_COLOR;
        
        if (i == 1) {
            UIButton *forgetButton = [[UIButton alloc] init];
            [self addSubview:forgetButton];
            [forgetButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(@(-30));
                make.top.equalTo(tf.mas_top);
                make.size.mas_equalTo(CGSizeMake(forget_width, 30));
            }];
            [forgetButton setTitle:@"忘记密码" forState:UIControlStateNormal];
            [forgetButton setTitleColor:BY_THEME_COLOR forState:UIControlStateNormal];
            forgetButton.titleLabel.font = FONT(15);
            [forgetButton addTarget:self action:@selector(forget) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    
    UIButton *loginButton = [[UIButton alloc] init];
    [self addSubview:loginButton];
    [loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@(-20));
        make.left.equalTo(@(30));
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 20 - 60, 35));
    }];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
    loginButton.titleLabel.font = FONT(14);
    loginButton.layer.cornerRadius = 4.0;
    loginButton.backgroundColor = BY_THEME_COLOR;
    [loginButton addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)forget{
    ByForgetVC *vc = [[ByForgetVC alloc] init];
    [(ByBaseVC *)self.viewController by_normalPush:vc hasNavi:YES];
}

#pragma mark - action
- (void)loginAction:(UIButton *)btn{
    
    UITextField *nameTf = [self viewWithTag:10];
    UITextField *passTf = [self viewWithTag:11];
    
    if (nameTf.text.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请输入手机号"];
        return;
    }
    
    if (nameTf.text.length != 11) {
        [ByCoreToolAlert showOnlyText:@"请输入正确的手机号"];
        return;
    }
    
    if (passTf.text.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请输入密码"];
        return;
    }
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    model.username = nameTf.text;
    model.password = passTf.text;
    [ByCoreToolAlert showLoadind:@"登录中..."];
    [ByApiUser login:model success:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showOnlyText:@"登录成功"];
        ByModelUser *user = (ByModelUser *)result.data;
        [ByCacheUser setUserInfo:user];
        [self.viewController dismissViewControllerAnimated:YES completion:nil];
        if (self.typeFrom.integerValue == 0) {
            UIWindow *window = [UIApplication sharedApplication].keyWindow;
            //        if (btn.tag == 10) {
            window.rootViewController = [[ByTabBarUser alloc] init];
            //        }else{
            //            window.rootViewController = [[ByTabBarDoctor alloc] init];
            //        }
        }
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showOnlyText:result.msg];
    }];
}

@end
